<?php

class ProxyController extends CController {

    public function actionIndex() {
        
    }
    
    public function actionGetListProxy() {
        $html = str_get_html(Parser::getHtmlPage('http://hideme.ru/proxy-list/?type=h&anon=4'));
        echo $html;
        if (!empty($html)) {
            $ips = array();
            foreach ($html->find('table.pl') as $table) {
                if (!empty($table)) {
                    foreach ($table->find('tr') as $tr) {
                        $ip = $tr->children(0)->plaintext;
                        if (preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $ip)) {
                            $ips[] = $ip;
                        }
                    }
                }
            }
            
            if (!empty($ips)) {
                foreach ($ips as $ip) {
                    $model = new Proxy();
                    $model->ip = $ip.":80";
                    $model->is_active = 0;
                    $model->save();
                    echo "$model->ip - добавлен!<br />";
                }
            }
        } else {
            echo "hideme.ru - недоступен!<br />";
        }
    }

    public function actionCheckProxy() {
                
        $criteria = new CDbCriteria;
        $criteria->limit = '5';
        $criteria->offset = '0';
        $criteria->order = 'is_active ASC';
        $models = Proxy::model()->findAll($criteria);
        
        if (!empty($models)) {
            foreach ($models as $model) {
                $html = str_get_html(Parser::getHtmlPage('http://www.wikipedia.org/', $model->ip));
                if (!empty($html)) {
                    $title = array_shift($html->find('title'))->innertext;
                    if($title == "Wikipedia"){
                        $model->is_active = 1;
                        $model->save();
                        echo "Прокси серверов " . $model->ip . " работоспособен!<br />";
                    } else {
                        $model->delete();
                        echo "Прокси серверов " . $model->ip . " удален!<br />";
                    }
                } else {
                    $model->delete();
                    echo "Прокси серверов " . $model->ip . " удален!<br />";
                }
            }
        } else {
            echo "Нет прокси серверов!<br />";
        }
    }

}